package cn.shenlong.portal.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.shenlong.common.Pagination;
import cn.shenlong.portal.domain.Link;
import cn.shenlong.portal.mapper.LinkMapper;
import cn.shenlong.portal.service.LinkService;
import cn.shenlong.util.UploadUtil;

@Service("LinkService")
public class LinkServiceImpl implements LinkService {

	@Autowired
	private LinkMapper LinkMapper;

	@Override
	@Transactional
	public void save(Link link, long linkId) {
		LinkMapper.saveEntity(link);
	}

	@Override
	public Pagination findLink(Link link, Map<String, Object> otherParm,
			Integer pageSize, Integer pageNo) {
		Pagination p = new Pagination();
		List<Link> contentList = LinkMapper.findPageListByCriteria(
				initlinkParm(link, otherParm),
				initRowBounds(pageSize, pageNo, p));
		Long count = LinkMapper.findCountByCriteria(initlinkParm(link,
				otherParm));
		p.setList(contentList);
		p.setTotalCount(count.intValue());
		return p;
	}

	private Map<String, Object> initlinkParm(Link link,
			Map<String, Object> otherParm) {
		Map<String, Object> parm = new HashMap<String, Object>();
		if (otherParm != null) {
			parm.putAll(otherParm);
		}
		if (link == null) {
			return parm;
		} else {
			parm.put("linkId", link.getLinkId());
			parm.put("linkType", link.getLinkType());
			parm.put("linkName", link.getLinkName());
			parm.put("linkUrl", link.getLinkUrl());
			parm.put("imageUrl", link.getImageUrl());
			return parm;
		}

	}

	private RowBounds initRowBounds(Integer pageSize, Integer pageNo,
			Pagination p) {
		RowBounds rowBounds = null;
		if (pageSize == null || pageNo == null) {
			rowBounds = new RowBounds();
		} else {
			p.setPageNo(pageNo);
			p.setPageSize(pageSize);
			rowBounds = new RowBounds(p.getFirstResult(), p.getPageSize());
		}
		return rowBounds;
	}

	@Override
	public Link findLinkByLinkId(long linkId) {
		return LinkMapper.findByPK(linkId);
	}

	@Override
	public Link updateLink(Link link) {
		int i = LinkMapper.updateEntity(link);
		if (i < 0)
			return null;
		return LinkMapper.findByPK(link.getLinkId());
	}

	@Override
	public void deleteLink(Long[] linkId) {
		Link link;
		for (Long id : linkId) {// 删除之前，先删除链接图片
			link = this.findLinkByLinkId(id);
			if (link != null && !StringUtil.isBlank(link.getImageUrl())) {
				UploadUtil.deleteForUrl(link.getImageUrl());
			}
		}
		LinkMapper.deleteByPKArray(linkId);
	}

	@Override
	public void deleteByLinkId(List<Long> LinkId) {
		LinkMapper.deleteByPKArray((Long[]) LinkId.toArray());
	}

	public void save(Link link) {
		LinkMapper.saveEntity(link);
	}
}
